package com.code.ribbon.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author liyufei
 * @since 2018-12-07 22:14
 */
@RestController
public class ConsumerController {

    @Value("${server.port}")
    private String port;
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "invokeError")
    @GetMapping("/consumer")
    public String consumer(@RequestParam("name") String name) {
        System.out.print("abc");
        System.out.println("测试ribbon：" + port);
        return restTemplate.getForObject("http://PRODUCE01/index?name=" + name + "&port=" + port, String.class);
    }

    public String invokeError(String name) {
        return "hi," + name + ",sorry,error!";
    }
}
